package com.gitee.lougnib.simplekits.quick_generator.common.easypoi;

import java.io.File;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public final class EasypoiKit {

	@SuppressWarnings("deprecation")
	public static <T> List<T> importExcel(int titleRows, int headRows, int sheetIndex, File file, Class<T> clazz) {
		ImportParams params = new ImportParams();
		params.setTitleRows(titleRows);
		params.setHeadRows(headRows);
		params.setStartSheetIndex(sheetIndex);
		params.setSheetNum(1);
		// 必须确保第一列有值，否则导入不进来
		params.setKeyIndex(0);
		List<T> resultList = ExcelImportUtil.importExcel(file, clazz, params);
		
		if (log.isDebugEnabled()) {
			log.debug("导入的记录数：{}", resultList.size());
			if (resultList.size() > 0) {
				log.debug("第一条记录的内容：{}", ToStringBuilder.reflectionToString(resultList.get(0)));
			}
		}
		
		return resultList;
	}
	
}
